How Does It Work?
What is PWM?
Pulse Width Modulation (PWM) is a technique that allows you to simulate an analog output using digital signals. Instead of sending a constant voltage, the Tamra board rapidly switches the digital output pin between HIGH and LOW states at a high frequency. The proportion of time the signal stays HIGH compared to the total cycle time is called the duty cycle.
Duty Cycle
- 100% duty cycle: The pin remains HIGH all the time, producing a constant voltage.
- 75% duty cycle: The pin is HIGH for 75% of the time and LOW for 25% of the time.
- 50% duty cycle: The pin is HIGH half the time and LOW half the time.
- 25% duty cycle: The pin is HIGH for 25% of the time and LOW for 75% of the time.
- 0% duty cycle: The pin remains LOW all the time, producing no voltage.
The duty cycle determines the average voltage and is perceived as different levels of brightness, speed, or sound intensity, depending on the connected component.
How PWM Works with LEDs
In this tutorial, we use PWM to control the brightness of a LED. The LED is connected to a Tamra board PWM-enabled pin on one side and to GND on the other side. A 220 ohms (Ω) resistor is placed in series with the LED to limit the current.
Behavior
When the duty cycle is 0%:
- The pin is always LOW.
- The LED is OFF.
When the duty cycle is 25%:
- The pin is HIGH for 25% of the time and LOW for 75% of the time.
- The LED appears dim.
When the duty cycle is 50%:
- The pin alternates equally between HIGH and LOW states.
- The LED appears moderately bright.
When the duty cycle is 75%:
- The pin is HIGH for 75% of the time and LOW for 25% of the time.
- The LED appears bright.
When the duty cycle is 100%:
- The pin is always HIGH.
- The LED is fully bright.
How It Works:
The LED's brightness is proportional to the average voltage delivered to it. By adjusting the duty cycle of the PWM signal, you control how bright the LED appears.
Visualization
Graphs
PWM Signal:
- Shows the HIGH and LOW states over time, varying with the duty cycle.
Brightness vs. Duty Cycle:
- Brightness is 0% at 0% duty cycle.
- Brightness increases proportionally with the duty cycle.
- Brightness is 25% at 25% duty cycle.
- Brightness is 75% at 75% duty cycle.
- Brightness is 100% at 100% duty cycle.
Summary
- PWM allows digital pins to simulate analog output by switching rapidly between HIGH and LOW states.
- The duty cycle determines the average voltage output and is perceived as varying brightness, speed, or intensity in connected components.
- LEDs appear brighter or dimmer based on the duty cycle of the PWM signal.
This tutorial focused on controlling a LED using PWM. You can apply the same concept to control motors, servos, and other components in more advanced applications!